package com.outfood.task;

import com.alibaba.fastjson.JSON;
import com.outfood.api.controller.StoreController;
import com.outfood.common.common.Result;
import com.outfood.common.common.response.RestResponse;
import com.outfood.domain.FoodAutoLoginStore;
import com.outfood.domain.Store;
import com.outfood.service.IStoreService;
import com.outfood.service.impl.ElmeOperateServiceImpl;
import com.outfood.service.impl.IFoodAutoLoginStoreService;
import com.outfood.service.impl.SeleniumServiceImpl;
import com.rys.common.enums.SearchTypeEnum;
import com.rys.common.utils.Search;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author huisonglin
 * @Description 重新登录定时器
 * @PackageName outfood-platform
 * @Package com.outfood.task
 * @Date 2024-04-11 9:12
 * @Version 1.0
 */
@Component
@Log4j2
public class AutoReLoginTask {

    @Resource
    ElmeOperateServiceImpl elmeOperateService;

    @Resource
    StoreController storeController;
    
    @Resource
    IStoreService storeService;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    IFoodAutoLoginStoreService foodAutoLoginStoreService;


    //   @PostConstruct
    @Scheduled(cron = "0 0/3 * * * ?")
    public void scan() {
        log.info("--------------------------------------------开始扫描所有离线的elem的门店并且开始尝试登录------------------------------------------------------------");
        long start = new Date().getTime();
        log.info("--------------------------------------------开始获取饿了么待出餐订单------------------------------------------------------------");//
        // List<Store> storeList = storeService.getList(new Search().add("id",SearchTypeEnum.In, Arrays.asList(55,87)));
        List<FoodAutoLoginStore> autoLoginStoreList = foodAutoLoginStoreService.getList(new Search().add("autoResult", SearchTypeEnum.EqualTo, "NORMAL"));
        log.info("获取到的满足条件的数据为{}", autoLoginStoreList);
        autoLoginStoreList.forEach(autoLoginStore -> {
            try {
                log.info("开始自动扽登录");
                Store store = storeService.get(autoLoginStore.getStoreId());
                if ("2".equals(store.getStoreStatus())) {
                    log.info(store.getStoreName() + "的storestatus为" + store.getStoreStatus() + "不符合条件不用自动登录");
                    return;
                }
                if ("ONLINE".equals(store.getOnlineStatus())) {
                    log.info(store.getStoreName() + "OnlineStatus" + store.getOnlineStatus() + "不符合条件不用自动登录");
                    return;
                }
                log.info("开始执行自动登录");
                SeleniumServiceImpl seleniumService = new SeleniumServiceImpl();
                try {
                    SeleniumServiceImpl.CodeLoginResult codeLoginResult = seleniumService.elemePassword(store.getUsername(), store.getPassword(), 19L, storeService, redisTemplate);
                    if (!"0".equals(codeLoginResult.getCode())) {
                        autoLoginStore.setAutoResult(codeLoginResult.getMessage());
                        foodAutoLoginStoreService.update(autoLoginStore.getId(), autoLoginStore, "task");
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    autoLoginStore.setAutoResult(e.getMessage());
                    foodAutoLoginStoreService.update(autoLoginStore.getId(), autoLoginStore, "task");
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }

        });
    }

    @Data
    public static class Res{
        private Storage storage;
    }

    @Data
    public static class Storage{
        private String ksid;
        private String shopId;
        private String shopName;
    }
}
